//
//  CAPurifierDetailsVC.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/7/26.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CAPurifierDetailsVC.h"
#import "CHGradientCircleView.h"
#import "SHLoginCMSModle.h"

@interface CAPurifierDetailsVC ()<UIPickerViewDataSource,UIPickerViewDelegate,sendPuidStateDelegate>{
    double angle;
}

@property (weak, nonatomic) IBOutlet UIImageView *centerImageView;
@property (weak, nonatomic) IBOutlet UILabel *centerLabel;

@property (weak, nonatomic) IBOutlet UILabel *PMLabel;
@property (weak, nonatomic) IBOutlet UILabel *PMEvaluationLabel;
@property (weak, nonatomic) IBOutlet UILabel *PMValue;



@property (weak, nonatomic) IBOutlet UIButton *lock;//童锁
@property (weak, nonatomic) IBOutlet UILabel *VOCLabel;//VOC
@property (weak, nonatomic) IBOutlet UILabel *cleanliness;//结晶剩余


@property (weak, nonatomic) IBOutlet UIButton *openOrCloseBtn;

@property (nonatomic, strong) CHGradientCircleView *CHGview;//圆环

@property (weak, nonatomic) IBOutlet UIImageView *returnImageView;


@property (weak, nonatomic) IBOutlet UIView *myView;//设备显示view


@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@property (nonatomic,strong)NSArray * letter;//保存要展示关机时间
@property (nonatomic,strong)NSArray * number;//保存要展示的风速

@end

@implementation CAPurifierDetailsVC

#pragma mark -- 懒加载
- (CHGradientCircleView *)CHGview{
    
    if (_CHGview == nil) {
        CGFloat CHGviewH = MAIN_HEIGHT * 0.32983508;
        self.CHGview = [[CHGradientCircleView alloc] initWithFrame:(CGRectMake(0, 0, CHGviewH, CHGviewH))];
        CGPoint CHGviewCenter = self.myView.center;
        CHGviewCenter.y -= 64;
        self.CHGview.center = CHGviewCenter;
        
        
    }
    return _CHGview;
}

#pragma mark -- 添加环行动画
- (void)addCHGview{
    
//    angle = -50;
    if (self.CHGview.superview == nil) {
        [self transform];//启动旋转动画
        [self.myView addSubview:self.CHGview];//添加圆环
    }else{
        [self.CHGview removeFromSuperview];//移除圆环
    }
}

#pragma mark -- 旋转
- (void)transform{
    
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.CHGview.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
        
    } completion:^(BOOL finished) {
        angle -= 50;

        if (self.CHGview.superview != nil) {
            [self transform];
        }
     
    }];
    
}

#pragma mark 加载数据
-(void)loadData{
    //需要展示的数据以数组的形式保存
    self.letter = @[@"4时",@"6时",@"7时",@"8时",@"10时",@"11时",@"12时"];
    self.number = @[@"1档",@"2档",@"3档",@"4档"];
}

- (void)returnImageViewClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    
    [self.returnImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnImageViewClick)]];
    
//    NSLog(@"%f,%f",self.myView.width,self.myView.height);
    
    //获取需要展示的数据
    [self loadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginNotice:) name:@"tongzhiLogin" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(StateNotice:) name:@"tongzhi" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(olderNotice:) name:@"sendOlder" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outNotice:) name:@"ForcedOut" object:nil];
    
    
    [SHLoginCMSModle shareInstence].sendPUdelegate = self;
    
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
//    self.pickView
    // Do any additional setup after loading the view from its nib.
}


-(void)sendPuid:(NSString*)PuidString sendState:(NSString*)StateString
   sendSequence:(NSString*)sequenceStr{
    
    NSLog(@"%@----%@----%@",PuidString,StateString,sequenceStr);
}

//传值令
-(void)sendOlder:(NSString*)OlderStr{
    
    NSLog(@"%@",OlderStr);
    
}


#pragma mark -- 发送命令 & 通知返回数据
#pragma mark -- 关闭
- (void)sendOrderWithOrder:(NSString *)order{
    //order－命令（字符串） puid－设备的puid（int）
    //NSLog(@"%@",_puid);
    
//    order = @"F1F1010001027E";
//    order = @"F1F10100017E";
//    NSString *order1 = @"0x01";
    NSLog(@"%@---%@",self.model.ids,order);
    NSNumber * puidValue = [NSNumber numberWithInteger:[self.model.ids integerValue]];
    
    [[SHLoginCMSModle shareInstence] sendOlderToPu:order getPuid:puidValue];
    
}

-(void)olderNotice:(NSNotification*)text
{
    
    if ([[[UIDevice currentDevice]systemVersion] floatValue] < 8.0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:text.userInfo[@"Older"] delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [alert show];
        });
        //通知返回数据
       NSLog(@"%@",text.userInfo[@"Older"]);
        
    }
    else
    {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text.userInfo[@"Older"] preferredStyle:UIAlertControllerStyleAlert];
        
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:okAction];
        
        NSLog(@"%@",text.userInfo[@"Older"]);
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self presentViewController:alert animated:YES completion:nil];
            
        });
    }
    
}


#pragma mark ------------ 设备功能
#pragma mark -- 开启，关闭设备
- (IBAction)openOrCloseEquipment:(UIButton *)sender {
    
    [self sendOrderWithOrder:@""];
    
    NSLog(@"%ld",(long)sender.tag);
    if (sender.tag) {
        //开启设备
        self.openOrCloseBtn.tag = 0;
        
        [self.openOrCloseBtn setBackgroundImage:[UIImage imageNamed:@"Remote_ShutDown"] forState:(UIControlStateNormal)];
        self.centerImageView.image = [UIImage imageNamed:@"Remote_NO"];
        
    }else{
        
        [self.openOrCloseBtn setBackgroundImage:[UIImage imageNamed:@"Remote_open"] forState:(UIControlStateNormal)];
        
        self.centerImageView.image = [UIImage imageNamed:@"Remote_Yes"];
        self.openOrCloseBtn.tag = 1;

    }
    
    self.centerLabel.hidden = !self.centerLabel.hidden;
    self.PMLabel.hidden = !self.PMLabel.hidden;
    self.PMEvaluationLabel.hidden = !self.PMEvaluationLabel.hidden;
    self.PMValue.hidden = !self.PMValue.hidden;
    
    self.lock.hidden = !self.lock.hidden;
    self.VOCLabel.hidden = !self.VOCLabel.hidden;
    self.cleanliness.hidden = !self.cleanliness.hidden;
    
    [self addCHGview];
}

#pragma mark -- 童锁
- (IBAction)lockWithEquipment:(UIButton *)sender {
      NSLog(@"%ld",(long)sender.tag);
    if (sender.tag) {
        self.lock.tag = 0;
        [self.lock setBackgroundImage:[UIImage imageNamed:@"Remote_lock_close"] forState:(UIControlStateNormal)];
    }else{
        self.lock.tag = 1;
        [self.lock setBackgroundImage:[UIImage imageNamed:@"Remote_lock_open"] forState:(UIControlStateNormal)];
    }
}

#pragma mark -- 净化器模式功能
- (IBAction)negativeIons:(UIButton *)sender {
    
    
    [self sendOrderWithOrder:@"F1F1020C020000000000000000000000107E"];
    NSLog(@"负离子");
}

- (IBAction)Ultraviolet_uv:(UIButton *)sender {
    
     [self sendOrderWithOrder:@"F1F10100017E"];
    NSLog(@"紫外线");
}

- (IBAction)ultra_quiet:(id)sender {
    
    
    [self sendOrderWithOrder:@"F1F1020C0200000000000000FF0000000F7E"];
    NSLog(@"超静音");
}

- (IBAction)manualMode:(UIButton *)sender {
    
    
    [self sendOrderWithOrder:@"F1F100017E"];
    NSLog(@"手动模式");
}

- (IBAction)intelligentModel:(UIButton *)sender {
    
    NSLog(@"智能模式");
}

- (IBAction)nightMode:(UIButton *)sender {
    
    NSLog(@"夜间模式");
}

#pragma mark UIPickerView DataSource Method 数据源方法

// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = @"";
    
    if (component == 1) {
        str = [self.letter objectAtIndex:row];
    } else if(component == 4) {
        str = [self.number objectAtIndex:row];
    }
    
    NSLog(@"%@",str);
}

//指定pickerview有几个表盘
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 6;//第一个展示字母、第二个展示数字
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return (0.224888 * MAIN_HEIGHT) / 3;
    
}

//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSInteger result = 1;
    switch (component) {
        case 1:
            result = self.letter.count;//根据数组的元素个数返回几行数据
            break;
        case 4:
            result = self.number.count;
            break;
            
        case 0:
        case 5:
            result = 1;
            break;
            
        case 2:
        case 3:
            result = 0;
            break;
            
        default:
            break;
    }
    return result;
}

#pragma mark UIPickerView Delegate Method 代理方法

//指定每行如何展示数据（此处和tableview类似）
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString * title = @"";
    switch (component) {
        case 1:
            title = self.letter[row];
            break;
        case 4:
            title = self.number[row];
            break;
            
        case 0:
            title = @"";
        case 5:
            title = @"";
            break;
            
        case 2:
        case 3:
            title = @"";
            break;
            
        default:
            break;
    }
    return title;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    
    UILabel *myView = nil;
    
    
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
    
    myView.textAlignment = NSTextAlignmentCenter;
    myView.textColor = [UIColor whiteColor];
    myView.text = [_letter objectAtIndex:row];
    
    if (MAIN_WIDTH < 375) {
        
        myView.font = [UIFont systemFontOfSize:13];         //用label来设置字体大小
    }else{
        
        myView.font = [UIFont systemFontOfSize:15];         //用label来设置字体大小
    }
    
    myView.backgroundColor = [UIColor clearColor];
    
    switch (component) {
        case 1:
            myView.text = self.letter[row];
            break;
        case 4:
            myView.text = self.number[row];
            break;
            
        case 0:
            myView.text = @"";
            break;
        case 5:
            myView.text = @"";
            break;
            
        case 2:
        case 3:
            myView.text = @"";
            break;
            
        default:
            break;
    }
    
    return myView;
    
}


//-------------------

-(void)loginNotice:(NSNotification *)text
{
    
   NSString* LoginStr = [NSString stringWithFormat:@"%@  cuid为%@",text.userInfo[@"Login"],text.userInfo[@"cuid"]];
    
    
    if ([[[UIDevice currentDevice]systemVersion] floatValue]<8.0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:LoginStr delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [alert show];
        });
        
        
    }
    else
    {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:LoginStr preferredStyle:UIAlertControllerStyleAlert];
        
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:okAction];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self presentViewController:alert animated:YES completion:nil];
            
        });
    }
    
    
}

-(void)StateNotice:(NSNotification*)text
{
    
    //_puid = text.userInfo[@"puid"];
    NSString *str = [NSString stringWithFormat:@"puid为%@的设备的在线状态为%@",text.userInfo[@"puid"],text.userInfo[@"state"]];
    
    if ([[[UIDevice currentDevice]systemVersion] floatValue]<8.0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [alert show];
        });
        
        
    }
    else
    {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:okAction];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self presentViewController:alert animated:YES completion:nil];
            
        });
    }
    
    
}

-(void)outNotice:(NSNotification*)text
{
    
    
    if ([[[UIDevice currentDevice]systemVersion] floatValue]<8.0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:text.userInfo[@"OutStr"] delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [alert show];
        });
        
        
    }
    else
    {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text.userInfo[@"OutStr"] preferredStyle:UIAlertControllerStyleAlert];
        
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:okAction];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self presentViewController:alert animated:YES completion:nil];
            
        });
    }
    
}


//-------------------


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
